package com.demo.studentmanager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.demo.studentmanager.dto.StudentAddDto;
import com.demo.studentmanager.dto.StudentListDto;
import com.demo.studentmanager.dto.StudentUpdDto;
import com.demo.studentmanager.entity.Student;
import com.demo.studentmanager.result.ResultCode;
import com.demo.studentmanager.service.IClassService;
import com.demo.studentmanager.service.IDepartmentService;
import com.demo.studentmanager.service.IStudentService;
import com.demo.studentmanager.util.CheckEntityUtil;
import com.demo.studentmanager.vo.StudentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.WebRequest;

import javax.validation.constraints.NotNull;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author LinQiSen
 */
@Api(tags = "学生模块")
@RestController
@RequestMapping("/student")
public class StudentController {

    @Autowired
    IStudentService studentService;
    @Autowired
    IClassService classService;
    @Autowired
    IDepartmentService departmentService;

    @RequiresAuthentication
    @GetMapping
    @ApiOperation(value = "查询学生信息")
    public IPage<StudentVO> getStudentList(@Validated StudentListDto studentListDto) {
        return studentService.getStudentList(studentListDto);
    }

    @RequiresRoles({"admin"})
    @PostMapping
    @ApiOperation(value = "增加学生信息")
    public void addStudent(@Validated StudentAddDto studentAddDto) {
        CheckEntityUtil.isNotNullById(studentAddDto.getSno(), studentService, ResultCode.STUDENT_ACCOUNT_EXIST);
        CheckEntityUtil.isNullById(studentAddDto.getClassno(), classService, ResultCode.CLASS_ACCOUNT_NOT_EXIST);
        CheckEntityUtil.isNullById(studentAddDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);
        Student student = new Student();
        studentAddDto.convertToEntity(student);
        studentService.save(student);
    }

    @RequiresAuthentication
    @PutMapping
    @ApiOperation(value = "修改学生信息")
    public void upStudent(@Validated StudentUpdDto studentUpdDto) {
        Student student = CheckEntityUtil.isNullById(studentUpdDto.getSno(), studentService, ResultCode.STUDENT_NOT_EXIST);
        if (studentUpdDto.getClassno() != null) {
            CheckEntityUtil.isNullById(studentUpdDto.getClassno(), classService, ResultCode.CLASS_ACCOUNT_NOT_EXIST);
        }
        if (studentUpdDto.getDeptno() != null) {
            CheckEntityUtil.isNullById(studentUpdDto.getDeptno(), departmentService, ResultCode.DEPARTMENT_NOT_EXIST);
        }
        studentUpdDto.convertToEntity(student);
        studentService.updateById(student);
    }

    @RequiresRoles({"admin"})
    @DeleteMapping("/{sno}")
    @ApiOperation(value = "禁用学生信息")
    public void delStudent(@NotNull @PathVariable String sno) {
        CheckEntityUtil.isNullById(sno, studentService, ResultCode.STUDENT_NOT_EXIST);
        studentService.removeById(sno);
    }

    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期 注意这里的转化要和传进来的字符串的格式一直 如2015-9-9 就应该为yyyy-MM-dd
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        // CustomDateEditor为自定义日期编辑器
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
}
